package com.edu.dao.impl;

import com.edu.dao.AnnouncementsDao;
import com.edu.entity.AnnouncementEntity;
import com.edu.utils.DatabaseManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * com.edu.dao.impl
 * 2024/7/14 22:46
 *
 * @author Fancy
 * @version 1.0
 */
public class AnnouncementsDaoImpl implements AnnouncementsDao {
    private Connection connection;


    public AnnouncementsDaoImpl() {
        try {
            this.connection = DatabaseManager.getInstance().getConnection();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public int createAnnouncement(AnnouncementEntity announcement) {
        String sql = "INSERT INTO Announcements (title, content, startDate, endDate, status) VALUES (?, ?, ?, ?, ?)";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, announcement.getTitle());
            statement.setString(2, announcement.getContent());
            statement.setObject(3, announcement.getStartDate());
            statement.setObject(4, announcement.getEndDate());
            statement.setString(5, announcement.getStatus());
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public List<AnnouncementEntity> readAllAnnouncements() {
        List<AnnouncementEntity> announcements = new LinkedList<>();
        String sql = "SELECT * FROM Announcements";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                AnnouncementEntity announcement = new AnnouncementEntity();
                announcement.setAnnouncementId(resultSet.getInt("announcementId"));
                announcement.setTitle(resultSet.getString("title"));
                announcement.setContent(resultSet.getString("content"));
                announcement.setStartDate(resultSet.getTimestamp("startDate"));
                announcement.setEndDate(resultSet.getTimestamp("endDate"));
                announcement.setStatus(resultSet.getString("status"));
                announcements.add(announcement);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return announcements;
    }

    @Override
    public int updateStatus(int announcementId, String newStatus) {
        String sql = "UPDATE Announcements SET status = ? WHERE announcementId = ?";
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, newStatus);
            statement.setInt(2, announcementId);
            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }
}
